package com.ym.medical.service.impl;

import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.medical.dao.AccountDao;
import com.ym.medical.domain.entity.AccountEntity;
import com.ym.medical.domain.vo.AccountVo;
import com.ym.medical.service.IAccountSV;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * 用户账户表Service业务层处理
 *
 * @author ym
 * @date 2020-08-18
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AccountSVImpl extends BaseSVImpl<AccountEntity, AccountDao> implements IAccountSV {


	/**
	 * 创建账户
	 *
	 * @param userId
	 * @return
	 */
	@Override
	public AccountEntity createAccount(Long userId) {
		AccountEntity search = new AccountEntity();
		search.setUserId(userId);
		AccountEntity accounts = mapper.selectOne(search);
		if (accounts == null) {
			accounts = new AccountEntity();
			accounts.setUserId(userId);
			accounts.setFreezeBalance(BigDecimal.ZERO);
			accounts.setAccBalance(BigDecimal.ZERO);
			accounts.setCoinBalance(BigDecimal.ZERO);
			mapper.insertSelective(accounts);
		}
		return accounts;
	}

	/**
	 * 更新用户的账户余额
	 *
	 * @param account
	 */
	@Override
	public void updateAccountById(AccountEntity account) {
		this.updateById(account);
	}

	@Override
	public AccountVo selectAccount(Long userId) {
		return mapper.selectAccountBalance(userId);
	}

	@Override
	public AccountEntity selectAccountByUserId(Long userId) {
		return null;
	}
}
